import { Component, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { OrganizationTreeComponent } from './organization-tree.component';
import { OrganizationUnitMembersComponent } from './organization-unit-members.component';
import { OrganizationUnitRolesComponent } from './organization-unit-roles.component';
import { IBasicOrganizationUnitInfo } from './basic-organization-unit-info';
import { TabDirective } from 'ngx-bootstrap/tabs';
import { AddMemberModalComponent } from './add-member-modal.component';
import { AddRoleModalComponent } from './add-role-modal.component';

@Component({
    templateUrl: './organization-units.component.html',
    animations: [appModuleAnimation()]
})
export class OrganizationUnitsComponent extends AppComponentBase {

    @ViewChild('ouMembers', {static: true}) ouMembers: OrganizationUnitMembersComponent;
    @ViewChild('ouRoles', { static: true }) ouRoles: OrganizationUnitRolesComponent;
    //@ViewChild('ouPost', { static: true }) ouPost: OrganizationPostComponent;
    @ViewChild('ouTree', { static: true }) ouTree: OrganizationTreeComponent;

    @ViewChild('addMemberModal', { static: true }) addMemberModal: AddMemberModalComponent;
    @ViewChild('addRoleModal', { static: true }) addRoleModal: AddRoleModalComponent;

    organizationUnit: IBasicOrganizationUnitInfo = null;

    selectedTabId: string = 'memberTab';

    constructor(
        injector: Injector
    ) {
        super(injector);
    }

    ouSelected(event: any): void {
        this.organizationUnit = event;
        this.ouMembers.organizationUnit = event;
        this.ouRoles.organizationUnit = event;
        //this.ouPost.organizationUnit = event;

        this.addMemberModal.organizationUnitId = this.organizationUnit.id;
        this.addRoleModal.organizationUnitId = this.organizationUnit.id;
    }

    onSelect(data: TabDirective): void {
        this.selectedTabId = data.id;
        //console.log(this.selectedTabId);
    }


    openAddMemberModal(): void {
        this.addMemberModal.show();
    }

    addMembers(data: any): void {
        this.ouTree.membersAdded({
            userIds: data.userIds,
            ouId: data.ouId
        });

        this.ouMembers.refreshMembers();
    }

    openAddRoleModal(): void {
        this.addRoleModal.show();
    }


    addRoles(data: any): void {
        this.ouTree.rolesAdded({
            roleIds: data.roleIds,
            ouId: data.ouId
        });

        this.ouRoles.refreshRoles();
    }
}
